﻿using PoemService.Shared;
using System.Data;
using Dapper;

namespace PoemService.Db
{
    public class PoemServiceDb:IPoemService
    {
        private readonly IDbConnection conn;
        public PoemServiceDb(IDbConnection _conn)
        {
            conn = _conn;
        }
        public async Task<bool> IsPoemLineExist(string line)
        {
            var obj = await GetPoemLine(line);
            return obj != null;
        }

        public async Task<PoemLine> GetPoemLine(string line)
        {
            return await conn.QueryFirstOrDefaultAsync<PoemLine>("select * from PoemLine where LineContent=@line", new { line });
        }

        public async Task<List<PoemLine>> GetPoemLineByPoemId(string poemId)
        {
            return (await conn.QueryAsync<PoemLine>("select * from PoemLine where PoemId=@poemId", new { poemId })).ToList();
        }

        public async Task<Poet> GetPoetByName(string name)
        {
            return await conn.QueryFirstOrDefaultAsync<Poet>("select * from Poet where Name=@name", new { name });
        }
    }
}